﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SOMClustering
{
    class ImagePattern: PatternBase, IClasterable
    {
        const int patternLength = 3;
        String imageName;
        String thumbFileName;
        Bitmap thumbnail;

        Point coordinates;

        public Point ClusterCoordinates
        {
            get { return coordinates; }
            set { coordinates = value; }
        }

        public ImagePattern(String imageName)
        {
            pattern = new Double[patternLength];
            this.imageName = imageName;
            Analyze();
            coordinates = new Point(0, 0);
            CreateThumbFileName();
        }

        private void CreateThumbFileName()
        {
            thumbFileName = ImageContainer.FilePathToName(imageName);
        }

        public ImagePattern(Double[] pattern, Bitmap thumbnail, String imageName)
        {
            this.pattern = pattern;
            this.thumbnail = thumbnail;
            this.imageName = imageName;
            coordinates = new Point(0, 0);
            CreateThumbFileName();
        }

        public String ImageName
        {
            get
            {
                return imageName;
            }
        }

        public Bitmap Thumbnail
        {
            get
            {
                return thumbnail;
            }
        }

        public String ThumbnailName
        {
            get
            {
                return thumbFileName;
            }
        }

        public Bitmap Analyze()
        {
            double[] inputD = new double[768];
            double[] inputP = new double[3];

            for (int ii = 0; ii < 768; ii++)
                inputD[ii] = 0;

            Bitmap bmp = new Bitmap(imageName);

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            for (int x = 0; x < bytes-3; x += 3)
            {
                inputD[rgbValues[x + 2]]++;
                inputD[256 + rgbValues[x + 1]]++;
                inputD[512 + rgbValues[x + 0]]++;
            }

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

            // Конец кода анализа изображения

            // нормализация
            int n = bmp.Width * bmp.Height;
            for (int ii = 0; ii < 768; ii++)
                inputD[ii] = 1.0 * inputD[ii] / n;

            // нахождение "нормализованного мат ожидания" для каждого цвета
            for (int jj = 0; jj < 3; jj++)
            {
                for (int ii = 0; ii < 256; ii++)
                    inputP[jj] = inputP[jj] + inputD[ii + 256 * jj] * ii;
                pattern[jj] = inputP[jj] / 128;
            }

            //
            int tnWidth;
            int tnHeight;
            if (bmp.Width > bmp.Height)
            {
                tnWidth = 96;
                tnHeight = bmp.Height * 96 / bmp.Width;
            }
            else
            {
                tnHeight = 96;
                tnWidth = bmp.Width * 96 / bmp.Height;
            }

            Bitmap tn96 = new Bitmap(96, 96);
            Graphics grs = Graphics.FromImage(tn96);
            grs.DrawImage(bmp, (96 - tnWidth) / 2, (96 - tnHeight) / 2, tnWidth, tnHeight);

            bmp.Dispose();

            thumbnail = tn96;

            return tn96;
        }
    }
}